CREATE PROCEDURE AddWordsProc
	@ForeignLanguage nvarchar(50),
	@ForeignWordText nvarchar(50),
	@SwedishWordText nvarchar(50),
	@Description nvarchar(255)
    
	AS

	DECLARE
	 @LangID int = 0,
	 @L1Count int = 0,
	 @L2Count int = 0,
	 @WordID int = 0
	 BEGIN
		 
		 SELECT @LangID = ForeignLanguageID
		 FROM ForeignLanguages
		 WHERE Name = @ForeignLanguage

		 SELECT @L1Count = Count(*)
		 FROM SwedishWords sw
		 WHERE sw.SwedishWordText = @SwedishWordText AND
		 sw.Description = @Description
	 
		 SELECT @L2Count = Count(*)
		 FROM ForeignLanguageWords flw
			 JOIN ForeignLanguages fl ON flw.ForeignLanguageID=fl.ForeignLanguageID
			 JOIN SwedishWords sw ON sw.WordID=flw.WordID
		 WHERE fl.Name = @ForeignLanguage AND
		 flw.ForeignWordText = @ForeignWordText

		 IF @L1Count = 0 AND @L2Count = 0

		 BEGIN

			INSERT INTO SwedishWords(SwedishWordText,Description) 				
			VALUES(@SwedishWordText, @Description)
	 
			INSERT INTO ForeignLanguageWords(WordID, ForeignLanguageID, ForeignWordText) 				
			VALUES(@@IDENTITY, @LangID, @ForeignWordText)

			PRINT 'New word added succesfully'

		 END 

		  IF @L1Count = 1 AND @L2Count = 0
		  
		  BEGIN

			 SELECT @WordID = WordID
			 FROM SwedishWords sw
			 WHERE sw.SwedishWordText = @SwedishWordText 
			 AND sw.Description = @Description
			
			 INSERT INTO ForeignLanguageWords(WordID,ForeignLanguageID,ForeignWordText) 				
			 VALUES(@WordID,@LangID, @ForeignWordText)

			 PRINT 'New word added succesfully'

		  END	 
 END

